
from sqlalchemy import Column, String, Integer, Text, DateTime, ForeignKey, SmallInteger
from sqlalchemy.orm import relationship
from config.mysql_config import Base
from datetime import datetime
from pojo.type import Type

class PPT(Base):
    """PPT主表"""
    __tablename__ = 'ppt'

    ppt_id = Column(Integer, primary_key=True, autoincrement=True)
    ppt_name = Column(String(255), unique=True, nullable=False)
    ppt_pages = Column(Integer, nullable=False)
    user_id = Column(Integer, nullable=False)
    type_id = Column(Integer, ForeignKey('type.type_id'), nullable=False)
    ppt_state = Column(SmallInteger, default=0)
    ppt_video_path = Column(String(500))
    ppt_image_path = Column(String(500))
    ppt_video_duration = Column(Integer)
    ppt_createtime = Column(DateTime, default=datetime.now)
    ppt_updatetime = Column(DateTime, default=datetime.now, onupdate=datetime.now)
    del_flag = Column(SmallInteger, default=0)
    # 默认为1，代表是ai生成的
    is_ai = Column(SmallInteger, default=1)

    # 反向引用：ppt.slides 可访问所有幻灯片
    slides = relationship('Slide', back_populates='ppt', cascade="all, delete-orphan", order_by='Slide.slide_index')
    # 反向引用：ppt.type 可访问所属类型
    type = relationship('Type', back_populates='ppts')

    def __repr__(self):
        return f"<PPT(ppt_id={self.ppt_id},ppt_name='{self.ppt_name}',ppt_state={self.ppt_state})>"

    def to_dict(self):
        return {
            "ppt_id": self.ppt_id,
            "ppt_name": self.ppt_name,
            "ppt_pages": self.ppt_pages,
            "user_id": self.user_id,
            "type_id": self.type_id,
            "ppt_state": self.ppt_state,
            "ppt_video_path": self.ppt_video_path,
            "ppt_image_path": self.ppt_image_path,
            "ppt_video_duration": self.ppt_video_duration,
            "ppt_createtime": self.ppt_createtime.strftime('%Y-%m-%d %H:%M:%S') if self.ppt_createtime else None,
            "ppt_updatetime": self.ppt_updatetime.strftime('%Y-%m-%d %H:%M:%S') if self.ppt_updatetime else None,
            "del_flag": self.del_flag,
            "is_ai": self.is_ai
        }
